package com.dongrj.framework.starter.sms.client;

import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.dysmsapi20170525.models.SendSmsResponseBody;
import com.aliyun.teaopenapi.models.Config;
import com.dongrj.framework.core.utils.JsonUtils;
import com.dongrj.framework.starter.sms.SmsRequest;
import com.dongrj.framework.starter.sms.SmsResponse;
import com.dongrj.framework.starter.sms.enums.SmsEnum;
import com.dongrj.framework.starter.sms.properties.AliYunSmsProperties;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;
import java.util.Objects;


@Slf4j
public class ALiYunSmsClient extends AbstractSmsClient {

    public static final String ENDPOINT = "dysmsapi.aliyuncs.com";

    private final Client client;
    private final AliYunSmsProperties aliYunSmsProperties;

    public ALiYunSmsClient(AliYunSmsProperties aliYunSmsProperties) {
        this.aliYunSmsProperties = aliYunSmsProperties;
        com.aliyun.credentials.Client credential = new com.aliyun.credentials.Client();
        Config config = new Config();
        config.setCredential(credential);
        config.setAccessKeyId(this.aliYunSmsProperties.getAccessKeyId());
        config.setAccessKeySecret(this.aliYunSmsProperties.getAccessKeySecret());
        config.setEndpoint(ENDPOINT);
        try {
            this.client = new Client(config);
        } catch (Exception e) {
            throw new RuntimeException("阿里云Sms客户端构建失败!", e);
        }
    }

    @Override
    public SmsResponse doSend(SmsRequest smsRequest) {
        SendSmsRequest sendSmsRequest = toSendSmsRequest(smsRequest);
        SmsResponse smsResponse = SmsResponse.fail();
        SendSmsResponse sendSmsResponse = null;
        try {
            sendSmsResponse = client.sendSms(sendSmsRequest);
        } catch (Exception e) {
            log.error("doSend.e", e);
        }
        if (Objects.isNull(sendSmsResponse)) {
            return smsResponse;
        }
        Integer statusCode = sendSmsResponse.getStatusCode();
        SendSmsResponseBody body = sendSmsResponse.getBody();
        if (Objects.equals(statusCode, 200) && Objects.equals(body.getCode(), "OK")) {
            smsResponse.setCode(1);
            log.info("doSend.mobiles={}", smsRequest.getMobiles());
            return smsResponse;
        } else {
            log.warn("doSend.smsRequest={},body={}", smsRequest, body);
        }
        return smsResponse;
    }

    public SendSmsRequest toSendSmsRequest(SmsRequest smsRequest) {
        SendSmsRequest sendSmsRequest = new SendSmsRequest();
        sendSmsRequest.setPhoneNumbers(smsRequest.getMobiles());
        sendSmsRequest.setSignName(this.aliYunSmsProperties.getSignName());
        sendSmsRequest.setTemplateCode(smsRequest.getTemplateCode());
        Map<String, Object> templateMap = smsRequest.getTemplateMap();
        sendSmsRequest.setTemplateParam(JsonUtils.toJson(templateMap));
        return sendSmsRequest;
    }

    @Override
    public SmsEnum smsEnum() {
        return SmsEnum.ALiYun;
    }
}
